const router = require('koa-router')()
const CreateSequelize = require('./sequelize')
const {
    DataTypes
} = require("sequelize")
let sequelize = new CreateSequelize()
sequelize.init_sequeleze()
router.prefix('/ymz')
const infos = sequelize.db.define('infos', {
    info_id: {
        type: DataTypes.STRING,
        comment: ''
    },
    account: {
        type: DataTypes.STRING,
        comment: ''
    },
    pass: {
        type: DataTypes.STRING,
        comment: ''
    },
    name: {
        type: DataTypes.STRING,
        comment: ''
    },
    headPortrait: {
        type: DataTypes.STRING,
        comment: ''
    },
    site: {
        type: DataTypes.STRING,
        comment: ''
    },
    QRCode: {
        type: DataTypes.STRING,
        comment: ''
    },
    phoneNum: {
        type: DataTypes.BIGINT(11),
        comment: ''
    },
    friendCircle: {
        type: DataTypes.STRING(10000),
        comment: ''
    },
    sex: {
        type: DataTypes.STRING,
        comment: ''
    },
    signature: {
        type: DataTypes.STRING,
        comment: ''
    },
    collect: {
        type: DataTypes.STRING(1000),
        comment: ''
    }
}, {
    timestamps: false
})
const records = sequelize.db.define('records', {
    uuid: {
        type: DataTypes.STRING,
        comment: ''
    },
    from_id: {
        type: DataTypes.STRING,
        comment: ''
    },
    to_id: {
        type: DataTypes.STRING,
        comment: ''
    },
    time: {
        type: DataTypes.DATE,
        comment: ''
    },
    msg: {
        type: DataTypes.STRING(10000),
        comment: ''
    }
}, {
    timestamps: false
})
const friends = sequelize.db.define('friends', {
    uuid: {
        type: DataTypes.STRING,
        comment: ''
    },
    fromId: {
        type: DataTypes.STRING,
        comment: ''
    },
    toId: {
        type: DataTypes.STRING,
        comment: ''
    },
    remark: {
        type: DataTypes.STRING,
        comment: ''
    },
    msg: {
        type: DataTypes.STRING,
        comment: ''
    },
    state: {
        type: DataTypes.STRING,
        comment: ''
    },
    img: {
        type: DataTypes.STRING,
        comment: ''
    },
    time: {
        type: DataTypes.STRING,
        comment: ''
    },
    createtimes: {
        type: DataTypes.STRING,
        comment: ''
    },
    isOnlyChat: {
        type: DataTypes.BOOLEAN,
        comment: ''
    },
    isBlacklist: {
        type: DataTypes.BOOLEAN,
        comment: ''
    },
    isRender: {
        type: DataTypes.BOOLEAN,
        comment: ''
    }
}, {
    timestamps: false
})

// 获取数据
router.get('/getData', async (ctx, next) => {
    let infosData = await infos.findAll({})
    let friendData = await friends.findAll({})
    ctx.body = {
        infosData,
        friendData
    }
})

// 添加数据和同意拒绝
router.post('/addInfoData', async (ctx, next) => {
    let {
        obj
    } = ctx.request.body
    let friendData = await friends.findAll({
        where: {
            fromId: obj.fromId,
            toId: obj.toId
        }
    })
    // 同意
    if (obj.state == 0) {
        let friend = await friends.update({
            state: obj.state,
            isRender: 0
        }, {
            where: {
                uuid: obj.uuid
            }
        })
        if (!friend) {
            ctx.body = {
                message: '未找到',
                friend
            }
            return
        }
        ctx.body = {
            message: '修改成功',
            friend
        }
    }
    // 拒绝
    if (obj.state == 1) {
        let friend = await friends.update({
            state: obj.state,
            isRender: 0
        }, {
            where: {
                uuid: obj.uuid
            }
        })
        if (!friend) {
            ctx.body = {
                message: '未找到',
                friend
            }
            return
        }
        ctx.body = {
            message: '修改成功',
            friend
        }
    }
    let newData
    if (friendData.length === 0) {
        newData = await friends.create({
            "uuid": obj.uuid,
            "fromId": obj.fromId,
            "toId": obj.toId,
            "remark": obj.remark,
            "msg": obj.msg,
            "state": JSON.stringify(obj.state),
            "img": obj.img,
            "time": JSON.stringify(obj.time),
            "createtimes": obj.createtimes,
            "localName": obj.localName,
            "isOnlyChat": obj.isOnlyChat,
            "isBlacklist": obj.isBlacklist,
            "isRender": obj.isRender
        })
    }
    ctx.body = {
        message: '添加成功',
        newData
    }
})

// 修改备注
router.post('/setRemark', async (ctx, next) => {
    let { obj } = ctx.request.body
    let { remark, data } = obj

    let friend = await friends.update({
        remark
    }, {
        where: {
            uuid: data.uuid
        }
    })
    if (!friend) {
        ctx.body = {
            message: '未找到',
            friend
        }
        return
    }
    ctx.body = {
        message: '修改成功',
        friend
    }
})

// 设置黑名单
router.post('/setBlackList', async (ctx, next) => {
    let { flag, data } = ctx.request.body
    let friend = await friends.update({
        isBlacklist: flag
    }, {
        where: {
            uuid: data.uuid
        }
    })
    if (!friend) {
        ctx.body = {
            message: '未找到',
            friend
        }
        return
    }
    ctx.body = {
        message: '修改成功',
        friend
    }
})

// 删除好友
router.post('/delFriend', async (ctx, next) => {
    let obj = ctx.request.body
    let friendData = await friends.destroy({
        where: {
            uuid: obj.uuid
        }
    })
    console.log(friendData);

    ctx.body = "delFriend"
})


module.exports = router